#include "qrectwidget.h"

#include "../../libs/shared/property/qabstractproperty.h"
#include "../../libs/shared/qpropertyfactory.h"
#include "../../libs/shared/qcommonstruct.h"
#include "../../libs/shared/qhostfactory.h"


#include <QPainter>
#include <QGradient>

QRectWidget::QRectWidget(QWidget *parent) :
    QAbstractGraphicsWidget(parent)
{
}

void QRectWidget::setXRadius(int xRadius)
{
    if(xRadius<0)
    {
        xRadius=0;
    }
    if(m_x_radius!=xRadius)
    {
        m_x_radius=xRadius;
        update();
    }
}

int QRectWidget::xRadius()
{
    return m_x_radius;
}

void QRectWidget::setYRadius(int yRadius)
{
    if(yRadius<0)
    {
        yRadius=0;
    }
    if(m_y_radius!=yRadius)
    {
        m_y_radius=yRadius;
        update();
    }
}

int QRectWidget::yRadius()
{
    return m_y_radius;
}

void QRectWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing,m_antialiasing);
    QGradient g=property("fill_color").value<QGradient>();
    p.setPen(m_pen);
    p.setBrush(QBrush(g));
    QRect re=this->rect();

    re.adjust(m_pen_info.m_width/2,
              m_pen_info.m_width/2,
              -m_pen_info.m_width/2-1,
              -m_pen_info.m_width/2-1);

    if(m_x_radius==0 || m_y_radius==0)
    {
        p.drawRect(re);
    }
    else
    {
        p.drawRoundedRect(re,
                          m_x_radius,m_y_radius);
    }
}



QRectWidgetHost::QRectWidgetHost(QAbstractHost *parent):
    QAbstractGraphicsHost(parent)
{
}

void QRectWidgetHost::create_object()
{
    m_object=new QRectWidget;
    m_object->setObjectName("rectangularWidget");
}

QString QRectWidgetHost::get_show_name()
{
    return tr("Rectangular");
}

QString QRectWidgetHost::get_show_group()
{
    return tr("Base Graphics");
}

QString QRectWidgetHost::get_show_icon()
{
    return ":/base_grahpics_images/rectangular.png";
}

void QRectWidgetHost::init_property()
{
    QAbstractGraphicsHost::init_property();

    QAbstractProperty* pro;

    QRectWidget* wid=(QRectWidget*)m_object;

    pro=QPropertyFactory::create_property("Number");
    if(pro!=NULL)
    {
        pro->set_property("name","x_radius");
        pro->set_attribute("show_name",tr("X Radius"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("x_radius",0);
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Number");
    if(pro!=NULL)
    {
        pro->set_property("name","y_radius");
        pro->set_attribute("show_name",tr("Y Radius"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("y_radius",0);
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","fill_color");
        pro->set_attribute("show_name",tr("Fill Color"));
        pro->set_attribute("group","Attributes");
        QLinearGradient g;
        g.setColorAt(0,QColor(0,0,0,0));
        g.setColorAt(1,QColor(0,0,0,0));
        m_object->setProperty("fill_color",QVariant::fromValue<QGradient>(g));
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,100,50));
}

void QRectWidgetHost::setXRadius(int xRadius)
{
    set_property_value("x_radius",xRadius);
}

int QRectWidgetHost::xRadius()
{
    return get_property_value("x_radius").toInt();
}

void QRectWidgetHost::setYRadius(int yRadius)
{
    set_property_value("y_radius",yRadius);
}

int QRectWidgetHost::yRadius()
{
    return get_property_value("y_radius").toInt();
}

qRegisterHost("basegraphics.rectangular",QRectWidgetHost,5020)
